package com.hqyj.shop.controller;

import com.hqyj.shop.pojo.Order;
import com.hqyj.shop.service.imp.CartServiceImpl;
import com.hqyj.shop.service.imp.OrderServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/user/shop")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private CartServiceImpl cartService;

    //商品页面直接点击购买，新建订单
    @RequestMapping("/createOrderByGoods")
    public String createOrderByGoods(Integer userId,String name, double total, Integer goodsId) {
        Date date = new Date();
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        Random r = new Random();
        int ra = r.nextInt(10000);
        String orderNum = "2021" + (date1.getDate() + 1) + date1.getMonth() + userId + ra;
        Order order = new Order(0, orderNum, userId, "", "未支付", goodsId, "", 0.0, date1, "", total, "", 1);
        boolean b=orderService.creatOrder(order);
        return "redirect:/user/shop/showOrder?username="+name+"&userId="+userId;
    }

    //购物车点击购买，新建订单
    @RequestMapping("/createOrder")
    public String createOrder(Integer userId, double total, Integer goodsId, Integer number, String username, Integer cartId) {
        Date date = new Date();
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        Random r = new Random();
        int ra = r.nextInt(10000);
        String orderNum = "2021" + (date1.getDate() + 1) + date1.getMonth() + userId + ra;
        Order order = new Order(0, orderNum, userId, "", "未支付", goodsId, "", 0.0, date1, "", total, "", number);
        boolean a = orderService.creatOrder(order);
        if (a == true) {
            cartService.deleteOneOnCart(cartId);
        }
        return "redirect:/user/shop/cart?username=" + username;
    }

    //查询订单
    @RequestMapping("/showOrder")
    public String showOrder(Integer userId, String username, ModelMap modelMap) {
        List<Order> orders = orderService.showOrder(userId);
        modelMap.put("orders", orders);
        modelMap.put("username", username);
        return "shop_order";
    }

    //修改订单的状态
    @RequestMapping("/updateStatus")
    public void updateStatus(HttpServletResponse response, Integer orderId, String status) throws IOException {
        System.out.println(orderId);
        if (status.equals("未支付")) {
            status = "已付款";
            boolean b = orderService.updateOrderStatus(orderId, status);
            if (b == true) {
                response.getWriter().write("payOK");
            } else response.getWriter().write("fail");
        } else if (status.equals("已发货")) {
            status = "已完成";
            boolean b = orderService.updateOrderStatus(orderId, status);
            if (b == true) {
                response.getWriter().write("shOK");

            } else response.getWriter().write("fail");
        } else if (status.equals("已付款")) {
            //点击退款
            //删除订单
            status = "待退款";
            boolean b = orderService.updateOrderStatus(orderId, status);
            if (b == true) {
                response.getWriter().write("tk");
            }else response.getWriter().write("fail");
        }

    }
    //删除订单
    @RequestMapping("/deleteOne")
    public void deleteOneOrder(HttpServletResponse response,Integer orderId) throws IOException {
        System.out.println(orderId);
        boolean b = orderService.deleteOneOnOrder(orderId);
        if (b==true){
            response.getWriter().write("ok");
        }else response.getWriter().write("fail");
    }


}
